package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: tensquare
 * @description: 标签控制层
 * @author: 廖俊杰
 * @date: 2021/8/17 15:26
 **/
@RestController
@RequestMapping("/label")
@CrossOrigin
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 查询全部列表
     * @return
     */
    @GetMapping("")
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",labelService.findAll());
    }

    /**
     * 根据ID查询标签
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result  findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findById(id));
    }

    /**
     * 增加标签
     * @param label
     * @return
     */
    @PostMapping("")
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改标签
     * @param label
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Label label,@PathVariable String id){
        label.setId(id);
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 根据条件查询
     * @param map
     * @return
     */
    @PostMapping("/search")
    public Result findSearch(@RequestBody Map map){
        return new Result(true,StatusCode.OK,"查询成功",labelService.findSearch(map));
    }

    /**
     *  条件+分页查询
     * @param map
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@RequestBody Map map,@PathVariable int page,@PathVariable int size){
        Page<Label> pageList = labelService.findSearch(map, page, size);
        return new Result(true,StatusCode.OK,"查询成功", new PageResult<Label>(pageList.getTotalElements(),pageList.getContent()));
    }
}
